﻿using System;
using System.Drawing;
using PBO.Data;
using PBO.Network;
using PBO.Network.Enumerations;

namespace PBO.Game.BattleClass
{
    public partial class Battle
    {
        /// <summary>
        /// 伤害计算
        /// </summary>
        private int CalculateDamage(Move selMove, double typeEff, BattleType selMoveType, Pokemon user, Pokemon target, bool miss, bool multiTarget, int hit = 1)
        {
            int hP = 0;
            switch (selMove.Data.AddEffect2)
            {
                case MoveAdditionalEffect.必杀:
                    return CalculateOHKO(user, target);
                case MoveAdditionalEffect.对象HP二分:
                    return target.HealthPower != 1 ? target.HealthPower / 2 : 1;
                case MoveAdditionalEffect.莽撞:
                    if (user.HealthPower < target.HealthPower)
                    {
                        return (target.HealthPower - user.HealthPower);
                    }
                    this.Fail();
                    return 0;

                case MoveAdditionalEffect.刀背击:
                    if ((hP >= target.HealthPower) && !target.BattleState.substituted)
                    {
                        hP = target.HealthPower - 1;
                    }
                    return hP;

                case MoveAdditionalEffect.固定伤害40:
                    return 40;

                case MoveAdditionalEffect.固定伤害20:
                    return 20;

                case MoveAdditionalEffect.自身等级伤害:
                    return user.LV;

                case MoveAdditionalEffect.对象等级伤害:
                    return target.LV;

                case MoveAdditionalEffect.幻象波浪:
                    return Convert.ToInt32(Math.Truncate((double)((user.LV * 0.5) * this._random.Next(1, 4))));
            }
            double num13 = typeEff;
            if (num13 < 1.0)
            {
                if (!multiTarget)
                {
                    log.AppendMsg("WeakHurt0");
                    //this.log.AddText("这不是很有效...", Color.Red, false);
                }
                if (user.GetSelTrait(false) == Trait.TintedLens)
                {
                    typeEff *= 2.0;
                }
            }
            else if (num13 > 1.0)
            {
                if (!multiTarget)
                {
                    log.AppendMsg("SuperHurt0");
                    //this.log.AddText("这非常有效！", Color.Red, false);
                }
                if (user.GetSelTrait(false) != Trait.MoldBreaker)
                {
                    if (target.GetSelTrait(false) == Trait.SolidRock)
                    {
                        typeEff *= 0.75;
                    }
                    if (target.GetSelTrait(false) == Trait.Filter)
                    {
                        typeEff *= 0.75;
                    }
                }
                if (user.GetItem(false) == Item.ExpertBelt)
                {
                    typeEff *= 1.2;
                }
            }
            if (user.HaveType(selMoveType))
            {
                if (user.GetSelTrait(false) == Trait.Adaptability)
                {
                    typeEff *= 2.0;
                }
                else
                {
                    typeEff *= 1.5;
                }
            }
            double num10 = selMoveType.WeatherEffects(this.Ground.Weather);
            double num6 = 1.0;
            if (user.GetSelTrait(false) != Trait.MoldBreaker)
            {
                if ((target.GetSelTrait(false) == Trait.ThickFat) && ((selMoveType == BattleType.Ice) || (selMoveType == BattleType.Fire)))
                {
                    num6 *= 0.5;
                }
                if ((target.GetSelTrait(false) == Trait.Heatproof) && (selMoveType == BattleType.Fire))
                {
                    num6 *= 0.5;
                }
                if ((target.GetSelTrait(false) == Trait.DrySkin) && (selMoveType == BattleType.Fire))
                {
                    num6 *= 1.25;
                }
            }
            double d = this.CalculateMovePower(user, target, selMove, selMoveType);
            if (selMove.Data.AddEffect2 == MoveAdditionalEffect.三连踢)
            {
                d *= hit;
            }
            if (multiTarget && (selMove.Target == MoveTarget.二体))
            {
                d *= 0.75;
            }
            int num4 = 1;
            if ((d != 0.0) && !miss)
            {
                double num11 = user.GetCTOdds((selMove.Data.AddEffect2 == MoveAdditionalEffect.易CT) || (selMove.Data.AddEffect1 == MoveAdditionalEffect.易CT));
                if ((!target.MyTeam.Group.LuckyChant && (((target.GetSelTrait(false) != Trait.BattleArmor) && (target.GetSelTrait(false) != Trait.ShellArmor)) || (user.GetSelTrait(false) == Trait.MoldBreaker))) && (this._random.NextDouble() < num11))
                {
                    num4 = 2;
                    if (user.GetSelTrait(false) == Trait.Sniper)
                    {
                        num4 = 3;
                    }
                    log.AppendMsg("CT0");
                    //this.log.AppendText("会心一击！", Color.Red);
                    if (target.GetSelTrait(false) == Trait.AngerPoint)
                    {
                        target.RaiseTrait();
                    }
                }
            }
            double num3 = 1.0;
            double num5 = 1.0;
            if (selMove.Data.Category == MoveCategory.物理)
            {
                if (num4 == 1)
                {
                    num3 = user.GetAttack(false);
                    num5 = target.GetDefence(target, false);
                }
                else
                {
                    num3 = user.GetAttack(true);
                    num5 = target.GetDefence(target, true);
                }
                if (((user.GetSelTrait(false) == Trait.FlowerGift) || (((user.Friend != null) && (user.Friend.GetSelTrait(false) == Trait.FlowerGift)) && (user.Friend.HealthPower != 0))) && (this.Ground.Weather == Weather.Sunny))
                {
                    num3 *= 1.5;
                }
                if (((user.GetSelTrait(false) == Trait.Rivalry) && (user.Gender != PokemonGender.None)) && (target.Gender != PokemonGender.None))
                {
                    if (user.Gender == target.Gender)
                    {
                        num3 *= 1.25;
                    }
                    else
                    {
                        num3 *= 0.75;
                    }
                }
            }
            else
            {
                if (num4 == 1)
                {
                    num3 = user.GetSpAttack(false);
                    num5 = target.GetSpDefence(user, false);
                }
                else
                {
                    num3 = user.GetSpAttack(true);
                    num5 = target.GetSpDefence(user, true);
                }
                if ((this.Ground.Weather == Weather.SandStorm) && target.HaveType(BattleType.Rock))
                {
                    num5 *= 1.5;
                }
                if (((target.GetSelTrait(false) == Trait.FlowerGift) || (((target.Friend != null) && (target.Friend.GetSelTrait(false) == Trait.FlowerGift)) && (target.Friend.HealthPower != 0))) && (this.Ground.Weather == Weather.Sunny))
                {
                    num5 *= 1.5;
                }
            }
            double num9 = 1.0;
            if ((target.MyTeam.Group.Reflect && (selMove.Data.Category == MoveCategory.物理)) && (num4 == 1))
            {
                if (this.Mode == BattleMode.Single)
                {
                    num9 = 0.5;
                }
                else
                {
                    num9 = 0.66666666666666663;
                }
            }
            else if ((target.MyTeam.Group.LightScreen && (selMove.Data.Category == MoveCategory.特殊)) && (num4 == 1))
            {
                if (this.Mode == BattleMode.Single)
                {
                    num9 = 0.5;
                }
                else
                {
                    num9 = 0.66666666666666663;
                }
            }
            if (user.BattleState.assisted)
            {
                num9 *= 1.5;
            }
            if (selMove.Data.AddEffect2 == MoveAdditionalEffect.礼物)
            {
                int num12 = (int)Math.Log(_random.Next(10) + 1, 2);

                if (num12 == 1)
                {
                    target.HPRecover(target.HealthPower * 0.25);
                    log.AppendMsg("HpRecover", target.GetNameString());
                    //this.log.AddText(target.GetNameString() + "恢复了HP！");
                    return 0;
                }
                else
                {
                    d = num12 == 0 ? 120 : num12 == 2 ? 40 : 80;
                }
            }
            else if (selMove.Data.AddEffect2 == MoveAdditionalEffect.围攻)
            {
                this.log.AppendMsg("BeatUpAtk", user.CustomName);
                //this.log.AppendText(user.CustomName + "攻击了！");
                foreach (Pokemon pokemon in user.MyTeam.CanChangePokemons)
                {
                    if (pokemon != user)
                    {
                        num3 += pokemon.GetAttack(false);
                        this.log.AppendMsg("BeatUpAtk", pokemon.CustomName);
                        //this.log.AppendText(pokemon.CustomName + "攻击了！");
                    }
                }
            }
            double num8 = ((double)this._random.Next(0x55, 0x65)) / 100.0;
            num3 = Math.Truncate(num3);
            num5 = Math.Truncate(num5);
            d = Math.Truncate(d);
            hP = Convert.ToInt32(Math.Truncate((double)(((((((((((((user.LV * 0.4) + 2.0) * num3) * d) / num5) / 50.0) + 2.0) * typeEff) * num4) * num9) * num10) * num6) * num8)));
            if (hP == 0)
            {
                hP = 1;
            }
            return hP;
        }

        private int CalculateOHKO(Pokemon user, Pokemon target)
        {
            int hP = 0;
            if ((target.GetSelTrait(false) == Trait.Sturdy) && (user.GetSelTrait(false) != Trait.MoldBreaker))
            {
                this.Fail();
            }
            else if (target.BattleState.substituted)
            {
                return target.BattleState.subHP;
            }
            else
            {
                hP = target.HealthPower;
                log.AppendMsg("OHKO");
                //this.log.AddText("这是一击必杀！");
            }
            return hP;
        }

    }
}